//
//  AnnouncementTableViewController.swift
//  d11
//
//  Created by Wai Yin Chan on 4/10/2017.
//  Copyright © 2017 Tech_001. All rights reserved.
//

import Foundation
import UIKit

class AnnouncementTableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.estimatedRowHeight = 200.0
        self.tableView.rowHeight = UITableView.automaticDimension
        
        let topLogo = UIImage(named: "icon_nav_left_logo")
        let titleView = UIView()
        let topLogoView = UIImageView(image: topLogo)
        titleView.addSubview(topLogoView)
        topLogoView.contentMode = .scaleToFill
        topLogoView.frame = CGRect(x: -(137/2), y:-(35/2), width: 137, height: 35)
        navigationItem.titleView = titleView
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let announcements = GlobalInfo.gameInfoData?["announcements"] as? [[String:String]] {
            return announcements.count
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! AnnouncementTableViewCell
        
        if let announcements = GlobalInfo.gameInfoData?["announcements"] as? [[String:String]] {
            let announcement = announcements[indexPath.row]
           
            cell.titleLabel.text = announcement["title"]
            cell.contentLabel.text = announcement["content"]
            cell.bottomLabel.text = announcement["date"]
        }
        
        let stackView = TZStackView()
        stackView.axis = .vertical
        
        cell.containerView.addSubview(stackView)
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.anchors.topAnchor.constraintEqualToAnchor(anchor: cell.containerView.anchors.topAnchor).isActive = true
        stackView.anchors.bottomAnchor.constraintEqualToAnchor(anchor: cell.containerView.anchors.bottomAnchor).isActive = true
        stackView.anchors.leadingAnchor.constraintEqualToAnchor(anchor: cell.containerView.anchors.leadingAnchor).isActive = true
        stackView.anchors.trailingAnchor.constraintEqualToAnchor(anchor: cell.containerView.anchors.trailingAnchor).isActive = true
        
        stackView.addArrangedSubview(cell.titleView)
        stackView.addArrangedSubview(cell.textContentView)
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
